// pages/storeManage/commodity/commodity.js
import http from '../../../utils/http';
import util from '../../../utils/util.js';

import {
  GetStoreGoodsListApi,
  orderCouponPages,
  apiStoreGoodsList,
  couponOperation
} from "../../../utils/api.js"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    businessId: 1,
    list: [],
    total: 0,
    isLastPage: false,

    navList: [
      {
        name: '团购',
        id: 1
      },
      {
        name: '买单',
        id: 2
      },
    ],
    activeIndex: 0,
    selectList: [
      {
        name: '全部',
        id: 0
      },
      {
        name: '已上架',
        id: 1
      },
      {
        name: '未上架',
        id: 2
      },
      {
        name: '未提交',
        id: 0
      },
      {
        name: '审核中',
        id: 1
      },
      {
        name: '审核不通过',
        id: 3
      },
      {
        name: '审核通过',
        id: 2
      },
    ],
    selectIndex: 0,

    paramData: {
      active: 0,
      businessId: 0,
      goodsType: 2,
      page: 1,
      pageSize: 20,
      keyword: '',
      auditStatus: -1,//审核状态(-1.所有 0.草稿 1.待审核 1.待审核 2.通过 3.驳回
    },

    textList: [
      {
        title: '上架申请',
        con: '提交上架申请后，工作人员会在3个工作日内完成审核。'
      },
      {
        title: '下架申请',
        con: '提交下架申请后，工作人员会在3个工作日内完成审核。'
      },
      {
        title: '撤销审核',
        con: '确定撤销审核吗？'
      },
    ],
    operationShow: false,
    operIndex: 0,
    operData: {},
  },
  selectview(e) {
    this.setData({
      activeIndex: e.currentTarget.dataset.index,
      list: [],
      'paramData.page': 1,
      selectIndex:0
    })
    if (e.currentTarget.dataset.index == 0) {
      this.getStoreList();
    } else {
      this.setData({
        'paramData.auditStatus':-1,
        'paramData.active':0,
        
      })
      this.getStoreGoodList();
    }
  },
  selectCot(e) {
    const { index, id } = e.currentTarget.dataset

    switch (index) {
      case 0:
        this.setData({
          'paramData.active': 0,
          'paramData.auditStatus': -1,
        })
        break;
      case 1:
        this.setData({
          'paramData.active': 1,
          'paramData.auditStatus': -1,
        })
        break;
      case 2:
        this.setData({
          'paramData.active': 2,
          'paramData.auditStatus': -1,
        })
        break;
      default:
        this.setData({
          'paramData.active': 0,
          'paramData.auditStatus': id,
        })
        break;

    }
    this.setData({
      selectIndex: index,
      list: [],
      'paramData.page': 1,
    })

    this.getStoreList();

  },
  // 团购列表
  getStoreList() {
    orderCouponPages(this.data.paramData).then(res => {
      if (this.data.paramData.page == 1 && res.data.data.length <= 0) {
        this.setData({
          list: [],
        })
        return;
      }
      if (res.data.data.length > 0) {
        this.setData({
          list: this.data.paramData.page == 1?res.data.data:this.data.list.concat(res.data.data),
          isLastPage: res.data.is_last_page
        })
      }
      // this.setData({
      //   list: this.data.list.concat(res.data.data),
      //   isLastPage: res.data.is_last_page
      // })

    })
  },
  // 买单
  getStoreGoodList() {
    apiStoreGoodsList(this.data.paramData).then(res => {
      this.setData({
        list: this.data.list.concat(res.data.data),
        isLastPage: res.data.is_last_page
      })
    })
  },
  //  撤销审核
  cancelExamine(e) {
    const { id, operation, index } = e.currentTarget.dataset

    let data = {
      id: id,
      operation: operation,//操作：1.上架 2.下架 3.撤销审核
      index: index
    }



    this.setData({
      operationShow: true,
      operIndex: operation - 1,
      operData: data
    })
  },
  close() {
    this.setData({
      operationShow: false
    })
  },
  jumpSeal() {
    let string = `list[${this.data.operData.index}].auditStatus`
    couponOperation(this.data.operData).then(res => {
      switch (this.data.operData.operation) {
        case 1:
          this.setData({
            [string]: 1,
            operationShow:false
          })
          wx.showToast({
            title: '上架成功',
            icon: 'none'
          })
          break;
        case 1:
          this.setData({
            [string]: 1,
            operationShow:false
          })
          wx.showToast({
            title: '下架成功',
            icon: 'none'
          })
          break;
        case 3:
          this.setData({
            [string]: 0,
            operationShow:false
          })
          wx.showToast({
            title: '撤销成功',
            icon: 'none'
          })
          break;
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options, "options")
    this.setData({
      businessId: options.id,
      'paramData.businessId': options.id
    })
    
  },
  // 获取商品列表
  getGoodsList() {
    let that = this;
    let data = {
      page: this.data.page,
      pageSize: this.data.pageSize,
      businessId: this.data.businessId,
      active: 0,
    }
    GetStoreGoodsListApi(data).then(res => {
      var array = [];
      that.setData({
        total: res.data.total_count ? res.data.total_count : ''
      })
      if (res.data.data.length != 0) {
        that.data.isLoading ? array = res.data.data : array = that.data.list.concat(res.data.data)
        if (that.data.isLoading) {
          console.log("isLoading=1", that.data.isLoading)
          that.setData({
            list: array,
          })
          console.log(this.data.list, "list")
        } else {
          console.log("isLoading=n", that.data.isLoading)
          that.setData({
            list: array,
            loading: true, //把"上拉加载"的变量设为false，显示 
            isLoading: false // 初始后都不是第一次上拉
          })
        }
      } else {
        console.log("数组为空")
        that.setData({
          loading: false, //把"上拉加载"的变量设为true，隐藏
          loaded: true, //把"上拉加载完成"的变量设为false，显示
        })
      }
    }).catch(err => {
      console.log(err, "err")
    })

  },
  jumpPage(e) {
    console.log(e.currentTarget.dataset, "e.currentTarget.dataset")
    const { url, id } = e.currentTarget.dataset
    wx.navigateTo({
      url: url,
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getStoreList();
  },
  onPullDownRefresh() {
    console.log('11')
    let that = this
    wx.showNavigationBarLoading()
    wx.showLoading()
    
    that.setData({
      list: [],
      'paramData.page': 1,
    })

    if(this.data.activeIndex==0){
      this.getStoreList();
    }else{
      this.getStoreGoodList();
    }
    

    setTimeout(function () {
      // 取消标题栏进度条效果
      wx.hideNavigationBarLoading()
      wx.stopPullDownRefresh()
      wx.hideLoading()
    }, 1000)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    let that = this;
    // console.log(that.data.total, that.data.list.length)
    if (!this.data.isLastPage) {
      this.data.paramData.page++
      if (this.data.activeIndex == 0) {
        this.getStoreList();
      } else {
        this.getStoreGoodList();
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})